# -*- coding: UTF-8 -*-
from werkzeug.wrappers import Response

from core.utils.configure import Configure


class AppMiddleWare:
    """验证 APP 是否合法"""

    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        """根据读取的请求头部的数据和配置文件数据进行比对"""
        # app_key = environ.get('HTTP_X_APP_ID')
        # app_secret = environ.get('HTTP_X_APP_SECRET')
        # if environ.get('REQUEST_METHOD') == 'OPTIONS':
        #     return self.app(environ, start_response)
        # store_app_key = Configure.get('APP_KEY_LIST')
        # store_app_secret = Configure.get('APP_SECRET_LIST')
        # Configure.set('X_APP_ID', app_key)
        # Configure.set('X_APP_SECRET', app_secret)
        # if (
        #     store_app_key is not None
        #     and store_app_secret is not None
        #     and app_key in store_app_key
        #     and app_secret in store_app_secret
        # ):
        #     return self.app(environ, start_response)
        # else:
        #     res = Response(
        #         u'{"status": "forbidden", "message": "Please input correct appKey or appSecret"}',
        #         mimetype='text/json',
        #         status=401,
        #     )
        #     return res(environ, start_response)
        return self.app(environ, start_response)